﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Data;
using System.Drawing;
using Gencode.PrsCommon.App.AppData;
using Gencode.PrsCommon.App;

namespace Gencode.PrsWeb.Controls.GridControls
{
	public class GridColoring
	{
		public enum GridType { PlanDienst, PlanPerson, PlanSaldo };
		Color saturdayColor;
		Color sundayColor;

		public DataView PlanNames
		{
			get;
			set;
		}
		private Color SaturdayColor
		{
			get
			{
				if (saturdayColor.ToArgb() == 0)
					saturdayColor = Color.FromArgb(PlanOption.SaturdayColorRGB);

				return saturdayColor;
			}
		}
		private Color SundayColor
		{
			get
			{
				if (sundayColor.ToArgb() == 0)
					sundayColor = Color.FromArgb(PlanOption.SundayColorRGB);

				return sundayColor;
			}
		}

		public void ColorGrid(DataGrid grid, GridType type, int planID)
		{
			if (PlanNames != null && PlanNames.Count > 0)
			{
				int rgb;
				switch (type)
				{
					case GridType.PlanDienst:
						for (int i = 0; i < PlanNames.Count; i++)
						{
							rgb = int.Parse(PlanNames[i][8].ToString());
							if (rgb != 0 && rgb != -1)
								grid.Items[i].BackColor = Color.FromArgb(rgb);

							ColorCells((DataRowView)PlanNames[i], grid.Items[i], i, planID);
						}
						break;
					case GridType.PlanPerson:
						for (int i = 0; i < PlanNames.Count; i++)
						{
							rgb = int.Parse(PlanNames[i][8].ToString());
							if (rgb != 0 && rgb != -1)
								grid.Items[i].BackColor = Color.FromArgb(rgb);
						}
						break;
					case GridType.PlanSaldo:
						for (int i = 0; i < PlanNames.Count; i++)
						{
							rgb = int.Parse(PlanNames[i][8].ToString());
							if (rgb != 0 && rgb != -1)
								grid.Items[i].BackColor = Color.FromArgb(rgb);
						}
						break;
				}
			}
		}

		private void ColorCells(DataRowView row, DataGridItem gridItem, int rowIndex, int planID)
		{
			int prsschemaid = int.Parse(row[5].ToString());
			DataView rowDv = DataGenerator.GetPlanRowColors(planID, prsschemaid);
			string tmpDate = DateUtil.ConvertSqlToDate(row[6].ToString());
			int month = int.Parse(DateUtil.GetMonth(tmpDate));
			int year = int.Parse(DateUtil.GetYear(tmpDate));
			DateTime date;

			for (int i = 0; i < gridItem.Cells.Count; i++)
			{
				date = new DateTime(year,month,i+1);

				if (date.DayOfWeek == DayOfWeek.Saturday)
					gridItem.Cells[i].BackColor = SaturdayColor;
				else if (date.DayOfWeek == DayOfWeek.Sunday)
					gridItem.Cells[i].BackColor = SundayColor;

				if (rowDv != null && rowDv.Count > 0)
				{
					string test1 = date.ToShortDateString();
					string test2 = rowDv[0].Row[0].ToString();
					string test3 = DateUtil.ConvertSqlToDate(rowDv[0].Row[0].ToString());
					if (date.ToShortDateString() == DateUtil.ConvertSqlToDate(rowDv[0].Row[0].ToString()))
					{
						gridItem.Cells[i].BackColor = Color.FromArgb(int.Parse(rowDv[0].Row[1].ToString()));
						rowDv.Delete(0);
					}
				}
			}
		}
	}
}
